﻿using ATM.Common;
using ATM.Entities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATM.DAO
{
    public class CardDAO
    {
        private static SqlConnection con;
        public static Card GetCurrentCard(string cardId)
        {
            Card card = new Card();
            con = DataConnection.GetConnection();
            string query = String.Format("Select * From Cards Where CardNo like '{0}'", cardId);
            SqlDataAdapter da = new SqlDataAdapter(query, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                card.CardNo = dt.Rows[0]["CardNo"].ToString();
                card.PIN = dt.Rows[0]["PIN"].ToString();
                card.Status = Convert.ToInt32(dt.Rows[0]["Status"].ToString());
                card.AccID = dt.Rows[0]["AccountID"].ToString();
                card.StartDate = dt.Rows[0]["StartDate"].ToString();
                card.ExpiredDate = dt.Rows[0]["ExpiredDate"].ToString();
                card.Attempt = Convert.ToInt32(dt.Rows[0]["Attempt"].ToString());
            }
            return card;
        }

        public static int ExecuteCommand(string query, SqlParameter[] param)
        {
            try
            {
                con = DataConnection.GetConnection();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddRange(param);
                cmd.Connection = con;
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw new Exception(ex.Message); }
            finally
            {
                con.Close();
            }
        }
    }
}
